package com.example.day02;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    // 声明UI组件
    private EditText etUsername, etPassword, etConfirmPassword, etEmail;
    private Button btnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 绑定UI组件
        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);
        etConfirmPassword = findViewById(R.id.etConfirmPassword);
        etEmail = findViewById(R.id.etEmail);
        btnSubmit = findViewById(R.id.btnSubmit);

        // 设置按钮点击事件监听
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取用户输入
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();
                String confirmPassword = etConfirmPassword.getText().toString();
                String email = etEmail.getText().toString();

                // 校验输入
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) ||
                        TextUtils.isEmpty(confirmPassword) || TextUtils.isEmpty(email)) {
                    // 提示用户输入不能为空
                    Toast.makeText(MainActivity.this, "请填写所有字段", Toast.LENGTH_SHORT).show();
                } else if (!password.equals(confirmPassword)) {
                    // 提示密码和确认密码不一致
                    Toast.makeText(MainActivity.this, "密码不匹配，请重新输入", Toast.LENGTH_SHORT).show();
                } else if (!email.contains("@")) {
                    // 提示邮箱格式错误
                    Toast.makeText(MainActivity.this, "请输入有效的邮箱地址", Toast.LENGTH_SHORT).show();

                } else if (username.equals("测试")) {
                    Toast.makeText(MainActivity.this, "测试", Toast.LENGTH_SHORT).show();

                } else {
                    // 显示提交成功的对话框
                    showConfirmationDialog();
                }
            }
        });
    }

    // 显示确认对话框
    private void showConfirmationDialog() {
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("注册成功")
                .setMessage("您的注册信息已提交！")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 用户点击"确定"按钮时的操作
                        dialog.dismiss();
                    }
                })
                .setCancelable(false)  // 设置对话框不可取消
                .show();
    }
}
